package com.tablefood.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.tablefood.dao.CartDao;
import com.tablefood.domain.ShopCart;
import com.tablefood.service.CartService;
import com.tablefood.utils.PageCartBean;
import com.tablefood.utils.PageUserBean;

@Transactional
public class CartServiceImpl implements CartService{

	@Resource
	CartDao cartDao;
	
	@Override
	public ShopCart insertCart(ShopCart cart) {
		 
		return cartDao.insertCart(cart);
	}

	@Override
	public List searchCartByUserid(String userid) {
		 
		return cartDao.searchCartByUserid(userid);
	}

	@Override
	public List searchCartAboutProduct(String userid, String productid) {
		 
		return cartDao.searchCartAboutProduct(userid, productid);
	}

	@Override
	public ShopCart updateCart(ShopCart cart) {
		 
		return cartDao.updateCart(cart);
	}

	@Override
	public void delCart(String userid) {
		 cartDao.delCart(userid);
	}

	@Override
	public ShopCart searchOneCartByCartId(String cartid) {
		 
		return cartDao.searchOneCartByCartId(cartid);
	}

	@Override
	public boolean delOneCart(Long cartid) {
		// TODO Auto-generated method stub
		return cartDao.delOneCart(cartid);
	}

	@Override
	public PageCartBean queryForPage(String userid,int pageSize, int page) {
		
		final String hql = "from ShopCart u where u.shopUser.userid = ?"; // 查询语句
		
		int allRow = cartDao.getAllRowCount(hql,userid); // 总记录数
		
		int totalPage = PageCartBean.countTatalPage(pageSize, allRow); // 总页数
		final int offset = PageCartBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageCartBean.countCurrentPage(page); // 当前页
		List list = cartDao.queryForPage(hql,userid, offset, length); //

		// 把分页信息保存到Bean当中
		PageCartBean pageCartBean = new PageCartBean();
		pageCartBean.setPageSize(pageSize);
		pageCartBean.setCurrentPage(currentPage);
		pageCartBean.setAllRow(allRow);
		pageCartBean.setTotalPage(totalPage);
		pageCartBean.setList(list);
		pageCartBean.init();
		return pageCartBean;
	}

}
